package com.hongruan.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.hongruan.bean.Account;
import com.hongruan.common.Constants;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxy;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * session验证拦截器
 * @author ZhangWenLong
 *
 */
public class SessionValidationInterceptor extends AbstractInterceptor {
    private static final long serialVersionUID = 1256799642379397894L;

    @Override
    public String intercept(ActionInvocation invocation) throws Exception {
        ActionProxy actionProxy = invocation.getProxy();
        String methodName = actionProxy.getMethod();
        
        if(!"login".equals(methodName)){
        	HttpServletResponse response = ServletActionContext.getResponse();
        	HttpServletRequest request = ServletActionContext.getRequest();
        	
        	Account account = (Account) request.getSession().getAttribute(Constants.SESSION_USER_KEY);
        	 
        	if(account == null){
        		response.sendRedirect(Constants.SYSTEM_PATH + "/login.jsp");
                return null;
        	}
        }
        
        return invocation.invoke();
    }
}

